using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;
using CsharpPoker.Core;

namespace CsharpPoker
{
    /// <summary>
    /// The `StartScene` is just an option menu with a pertty bg and some flashy text related to CSP.
    /// </summary>
    public class StartScene : GameScene
    {
        // Used for handle input
        protected Input input;

        // Misc
        protected TextMenuComponent menu;
        
        // Audio
        //protected AudioLibrary audio;
        
        // Spritebatch
        protected SpriteBatch spriteBatch = null;
        
        // Gui Stuff
        protected Rectangle texasTextRect = new Rectangle(0, 0, 536, 131);
        protected Vector2 texasTextPosition;
        protected Rectangle holdemTextRect = new Rectangle(120, 165, 517, 130);
        protected Vector2 holdemTextPosition;

        // Textures
        //protected Texture2D helpBackgroundTexture, helpForegroundTexture;
        protected Texture2D background, elements;

        // Fonts
        private SpriteFont smallFont, largeFont; 

        // reference to the Game this scene is taking place in.
        Game game;
        SceneManager sceneManager;

        public StartScene(Game game, SceneManager sceneManager)
            : base(game)
        {
            this.game = game;
            this.sceneManager = sceneManager;

            // Get the audio library
            //audio = (AudioLibrary) Game.Services.GetService(typeof(AudioLibrary));
            input = new Input();
        }

        public override void Show()
        {
            //audio.whatever.Play();

            texasTextPosition.X = -1 * texasTextRect.Width;
            texasTextPosition.Y = 40;
            holdemTextPosition.X = Game.Window.ClientBounds.Width;
            holdemTextPosition.Y = 180;

            // Put the menu centered in screen
            menu.Position = new Vector2((Game.Window.ClientBounds.Width -
                                          menu.Width) / 2, 330);

            // These elements will be visible when the 'Rock Rain' title
            // is done.
            menu.Visible = false;
            menu.Enabled = false;

            base.Show();
        }

        public override void Hide()
        {
            MediaPlayer.Stop();
            base.Hide();
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {    
            // Create the Start Scene
            smallFont = Game.Content.Load<SpriteFont>("menuSmall");
            largeFont = Game.Content.Load<SpriteFont>("menuLarge");

            background = Game.Content.Load<Texture2D>("startbackground");
            elements = Game.Content.Load<Texture2D>("startSceneElements");
                    
            // adds the background image to the GameScene.component list.
            Components.Add(new ImageComponent(game, background, ImageComponent.DrawMode.Center));

            // Create the Menu
            string[] items = { "Play Holdem", "Help", "Quit" };
            menu = new TextMenuComponent(game, smallFont, largeFont);
            menu.SetMenuItems(items);
            Components.Add(menu);

            // Get the current spritebatch
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
          
            base.LoadContent();
        }

        public override void Input()
        {
            if (input.CheckEnter())
            {
                //audio.MenuSelect.Play();
                switch (SelectedMenuIndex)
                {
                    case 0:
                        sceneManager.TexasHoldem.RestartScene();
                        sceneManager.ShowScene(sceneManager.TexasHoldem);
                        break;
                    case 1:
                        //ShowScene(helpScene);
                        break;
                    case 2:
                        Game.Exit();
                        break;
                }
            }

            base.Input();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!menu.Visible)
            {
                // this code dynamically moves the title text into position.
                if (holdemTextPosition.X >= (Game.Window.ClientBounds.Width - 595)/2)
                {
                    holdemTextPosition.X -= 15;
                }

                if (texasTextPosition.X <= (Game.Window.ClientBounds.Width - 715)/2)
                {
                    texasTextPosition.X += 15;
                }
                else
                {
                    menu.Visible = true;
                    menu.Enabled = true;

                    //MediaPlayer.Play(audio.StartMusic);
                }
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            spriteBatch.Draw(elements, texasTextPosition, texasTextRect, Color.White);
            spriteBatch.Draw(elements, holdemTextPosition, holdemTextRect, Color.White);
        }

        /// <summary>
        /// Gets the selected menu option
        /// </summary>
        public int SelectedMenuIndex
        {
            get { return menu.SelectedIndex; }
        }
    }
}